package com.itheima.web.listener;

import com.itheima.service.store.QuestionItemService;
import com.itheima.service.store.impl.QuestionItemServiceImpl;
import com.itheima.service.system.RoleService;
import com.itheima.service.system.impl.RoleServiceImpl;
import lombok.extern.log4j.Log4j;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.time.LocalTime;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Log4j
@WebListener
public class CleanListener implements ServletContextListener {
    private QuestionItemService qis=new QuestionItemServiceImpl();
    private RoleService rs=new RoleServiceImpl();
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent){
        ScheduledExecutorService ses = Executors.newScheduledThreadPool(1);
        ses.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                log.info("********************************************************************************************");
                log.info("**************************************开始清空没有题目的选项************************************");
                log.info("********************************************************************************************");

                qis.clean();


                log.info("********************************************************************************************");
                log.info("**********************************开始清空与不存在的角色的中间表数据******************************");
                log.info("********************************************************************************************");

                rs.clean();
            }
        },
        (24 * 60 * 60) - LocalTime.now().toSecondOfDay(),
        (24 * 60 * 60),
        TimeUnit.SECONDS);
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
